var WxParse = require('../../common/wxParse/wxParse.js');

var App = getApp();
Page({
  data: {
    detail: {},
    cartCount: 0,
    reviews: {},
    specificationArray: [],
    indicatorDots: true,
    autoplay: false,
    interval: 3000,
    duration: 1000,
    tabs: ["商品详情", "商品评价"],
    activeIndex: 0,
    num: 1, // 商品数量
    minusStatuses: '',
    flag: 0,
    firstIndex: -1,
    attrValueList: [],
    productId: ''
  },
  onLoad: function (param) {
    console.log(param, "扫码参数param")
    if (param.id) {
      var productId = param.id
    }
    else {
      var str = param.scene
      var arr = []
      arr = str.split("_")
      var productId = arr[0]
      this.setData({
        qzz: arr[1]
      })
      console.log(arr)
    }
    this.setData({
      productId: productId
    })
 
    // 商品id
    this.getMuDetail(productId)

  },
  // 单独购买执行的参数
  dan: function (e) {
    console.log(e.currentTarget.dataset.ow)
    console.log(e.currentTarget.dataset.kai)
    var kait = e.currentTarget.dataset.kai
    var d = e.currentTarget.dataset.ow
    var zhuangtai = e.currentTarget.dataset.zt
    wx.setStorageSync('zhuangtai', zhuangtai)
    if (d == false) {
      this.toMerchants();
    } else {
      //  判断是否需要进店
      // 不需要进店
      var t = this.data.detail.thumbnail
      var i = this.data.detail.productId
      var q = this.data.detail.collagePrice
      var y = this.data.detail.primeCost
      var id = this.data.detail.id
      var s = JSON.stringify({
        t,
        i,
        q,
        y,
        kait,
        id
      })
      wx.navigateTo({
        url: '/pages/integral-mall/dandu/dandu?d=' + s,
      })
    }

  },

  getMuDetail(id) { // 拼团详情
    App.HttpService.getMuDetail({
      id: id,
      buyerId: this.data.qzz
    }).then(res => {
      let data = res.data
      if (data.code == 200) {
        this.setData({
          detail: data.data
        })

        WxParse.wxParse('article', 'html', data.data.introduction, this, 5) // 解析html代码 富文本显示
        let endTimeList = []
        let groupList = data.data.groupList
       
        if (groupList.length > 0) {
          groupList.forEach(o => {
            endTimeList.push(o.endDate)
          }) // 将活动的结束时间参数提成一个单独的数组，方便操作
          this.setData({
            actEndTimeList: endTimeList
          })
          this.countDown() // 执行倒计时函数
        }
      }
    })
  },
  groupBuy(e) { //去拼团

    var t = e.currentTarget.dataset.ow
    var kait = e.currentTarget.dataset.kai
    var pid = e.currentTarget.dataset.groupId
   
    var zhuangtai = e.currentTarget.dataset.zt
    wx.setStorageSync('zhuangtai', zhuangtai)
    if (t == false) {
      let {
        groupId
      } = e.currentTarget.dataset
      let {
        detail
      } = this.data
      let group = {
        groupId: groupId,
        multiGroupSetId: detail.id,
        name: detail.multiGroupName,
        image: detail.thumbnail,
        price: detail.collagePrice,
        qian: detail.primeCost,
        num: 1
      }
      wx.setStorageSync('group', group)
      wx.navigateTo({
        url: '/pages/merchants/list/index?productStatus=1'
      })
    } else {
      var t = this.data.detail.thumbnail
      var i = this.data.detail.productId
      var q = this.data.detail.collagePrice
      var y = this.data.detail.primeCost
      var id = this.data.detail.id
    
      var s = JSON.stringify({
        t,
        i,
        q,
        y,
        kait,
        id,
        pid
      })
      wx.navigateTo({
        url: '/pages/integral-mall/dandu/dandu?d=' + s,
      })
    }
  },
  toMerchants() { // 去选店
    let {
      detail
    } = this.data
    let group = {
      groupId: 0,
      multiGroupSetId: detail.id,
      name: detail.multiGroupName,
      image: detail.thumbnail,
      price: detail.collagePrice,
      qian: detail.primeCost,
      num: 1
    }
    wx.setStorageSync('group', group)
    wx.navigateTo({
      url: '/pages/merchants/list/index?productStatus=1'
    })
  },
  timeFormat(param) { // 小于10的格式化函数
    return param < 10 ? '0' + param : param;
  },
  countDown() { // 倒计时函数
    // 获取当前时间，同时得到活动结束时间数组
    let newTime = new Date().getTime()
    let endTimeList = this.data.actEndTimeList
    let countDownArr = []
    // 对结束时间进行处理渲染到页面
    endTimeList.forEach(o => {
      let endTime = new Date(o).getTime()
      let obj = null
      // 如果活动未结束，对时间进行处理
      if (endTime - newTime > 0) {
        let time = (endTime - newTime) / 1000
        // 获取天、时、分、秒
        let day = parseInt(time / (60 * 60 * 24))
        let hou = parseInt(time % (60 * 60 * 24) / 3600)
        let min = parseInt(time % (60 * 60 * 24) % 3600 / 60)
        let sec = parseInt(time % (60 * 60 * 24) % 3600 % 60)
        obj = {
          day: this.timeFormat(day),
          hou: this.timeFormat(hou),
          min: this.timeFormat(min),
          sec: this.timeFormat(sec)
        }
      } else { // 活动已结束，全部设置为'00'
        obj = {
          day: '00',
          hou: '00',
          min: '00',
          sec: '00'
        }
      }
      countDownArr.push(obj)
    })
    // 渲染，然后每隔一秒执行一次倒计时函数
    this.setData({
      countDownList: countDownArr
    })
    setTimeout(this.countDown, 1000)
  },
  // 生成海报
  sheng: function () {
    var i = this.data.detail.id
    var k = wx.getStorageSync('userId')
    var s = JSON.stringify({
      i,
      k
    })
    wx.navigateTo({
      url: '/pages/group-buying/haibao/haibao?p=' + s,
    })
    console.log(6)
  },
})